from utils.gum_algorithm23.parse_data import *
import shutil


def get_cumulative_data(filepath_dict):
    cumulative_data_dict = {}
    for key in filepath_dict:
        cumulative_data_dict[key] = accumulate_pulses(filepath_dict[key])
    return cumulative_data_dict


def accumulate_pulses(filepath_list):
    if filepath_list != []:
        data = parse_LD_data(filepath_list[0])
        Range = data[:, 0]
        signal_sum = sum([parse_LD_data(filepath)[:, 1:] for filepath in filepath_list])
        data = np.hstack((Range.reshape(-1, 1), signal_sum))
        # plt.figure()
        # plt.plot(data[:, 1:], data[:, 0])
        # plt.ylabel('Height/km')
        # plt.show()
    else:
        data = None
    return data


def find_L0_file(L2_filepath, L0_path):
    filename = os.path.basename(L2_filepath)
    fileinfo = filename.split("_")
    lid_code = fileinfo[1]
    # dst_path = r'D:\1.Work\1.Data\新型地基\数据\test_data2(少量)\{}'.format(lid_code)
    # if not os.path.exists(dst_path):
    #     os.mkdir(dst_path)
    if fileinfo[3].isdigit():
        detect_time = fileinfo[3]
    elif fileinfo[-2].isdigit():
        detect_time = fileinfo[-2]
    else:
        print("Filename Error: {}".format(L2_filepath))
    begin_time = int(detect_time) // 10000 * 10000
    end_time = (int(detect_time) // 10000 + 1) * 10000
    filepath_dict = {}
    if os.path.isfile(L0_path):
        filepath_list = [L0_path]
    elif os.path.isdir(L0_path):
        filepath_list = get_file_list(L0_path)
        if lid_code != "LD4":
            for key in [
                "Rm_DV",
                "Ry_DV",
                "Ry_DE",
                "Ry_DN",
                "Na_DV",
                "Na_DE",
                "Na_DN",
                "Fe_DV",
                "Fe_DE",
                "Fe_DN",
            ]:
                detect_type, direction = key.split("_")
                if direction == "DE":
                    direction_list = ["DE", "DW"]
                elif direction == "DN":
                    direction_list = ["DN", "DS"]
                else:
                    direction_list = [direction]
                data_list = [
                    filepath
                    for filepath in filepath_list
                    if detect_type
                    in [
                        os.path.basename(filepath).split("_")[-4],
                        os.path.basename(filepath).split("_")[3],
                    ]
                    and (
                        os.path.basename(filepath).split("_")[-3] in direction_list
                        or os.path.basename(filepath).split("_")[4] in direction_list
                    )
                    and begin_time
                    <= int(os.path.basename(filepath).split("_")[5].split(".")[0])
                    < end_time
                ]
                if data_list != []:
                    filepath_dict[key] = data_list
                    # for src_path in data_list:
                    #     shutil.copy(src_path, dst_path)
        else:
            data_list = [
                filepath
                for filepath in filepath_list
                if begin_time
                <= int(os.path.basename(filepath).split("_")[3].split(".")[0])
                < end_time
            ]
            if data_list != []:
                filepath_dict["Ry_DE"] = data_list
    else:
        print("L0_path ERROR!!!")
    return filepath_dict


# def preprocess(L2_filepath, L0_path, output_dirpath='ResultPic'):
#     data_L2 = parse_LD_data(L2_filepath)
#     filepath_dict = find_L0_file(L2_filepath, L0_path)
#     data_L0_dict = get_cumulative_data(filepath_dict)
#     # 画图，主要用于调试
#     # filename = os.path.basename(L2_filepath)
#     # fileinfo = filename.split('_')
#     # lid_code = fileinfo[1]
#     # for key in data_L0_dict:
#     #     data_L0 = data_L0_dict[key]
#     #     plt.figure()
#     #     plt.semilogx(data_L0[:, 1], data_L0[:, 0])
#     #     plt.xlabel('一小时累计光子数')
#     #     plt.ylabel('高度(km)')
#     #     plt.grid()
#     #     plt.ylim([0, 100])
#     #     plt.savefig(os.path.join(output_dirpath, lid_code + '累计光子数.png'))
#     return data_L2, data_L0_dict

# def find_L0_file(L2_filepath, L0_path):
#     # 只针对文件名来处理
#     # D
#     # {'Ry_DV': []}
#     # {'Ry_DV': []}
#     # {'Ry_DN': [], 'Ry_DE': []}
#     # T/P
#     # {'Ry_DV': [], 'Rm_DV': [], 'Fe_DN': [], 'Fe_DE': []}
#     # {'Ry_DV': [], 'Rm_DV': [], 'Na_DV': []}
#     # {'Ry_DN': [], 'Ry_DE': [], 'Na_DN': [], 'Na_DE': []}
#     # Na
#     # {'Na_DV': []}
#     # {'Na_DN': [], 'Na_DE': []}
#     # Fe
#     # {'Fe_DN': [], 'Fe_DE': []}
#     # W
#     # {'Ry_DE': [], 'Fe_DE': [],'Ry_DN': [], 'Fe_DN': []}
#     # {'Ry_DE': [], 'Na_DE': [],'Ry_DN': [], 'Na_DN': []}
#     # {'Ry_DE': [], 'Na_DE': [],'Ry_DN': [], 'Na_DN': []}
#     filename = os.path.basename(L2_filepath)
#     fileinfo = filename.split('_')
#     lid_code = fileinfo[1]
#     data_type = fileinfo[2]
#     detect_time = fileinfo[3]
#     begin_time = int(detect_time) // 10000 * 10000
#     end_time = (int(detect_time) // 10000 + 1) * 10000
#     filepath_dict = {}
#     if os.path.isfile(L0_path):
#         filepath_list = [L0_path]
#     elif os.path.isdir(L0_path):
#         filepath_list = get_file_list(L0_path)
#         if data_type == 'D':
#             if lid_code in ['LD1', 'LD2']:
#                 Ry_DV_list = [
#                     filepath for filepath in filepath_list if filepath.split('_')[-4] == 'Ry'
#                     and filepath.split('_')[-3] == 'DV' and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Ry_DV_list != []:
#                     filepath_dict['Ry_DV'] = Ry_DV_list
#             elif lid_code in ['LD3', 'L3A', 'L3B']:
#                 Ry_DE_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Ry' and filepath.split('_')[-3] in ['DE', 'DW']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Ry_DE_list != []:
#                     filepath_dict['Ry_DE'] = Ry_DE_list
#                 Ry_DN_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Ry' and filepath.split('_')[-3] in ['DN', 'DS']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Ry_DN_list != []:
#                     filepath_dict['Ry_DN'] = Ry_DN_list
#         elif data_type in ['T', 'P']:
#             if lid_code in ['LD1']:
#                 Ry_DV_list = [
#                     filepath for filepath in filepath_list if filepath.split('_')[-4] == 'Ry'
#                     and filepath.split('_')[-3] == 'DV' and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Ry_DV_list != []:
#                     filepath_dict['Ry_DV'] = Ry_DV_list
#                 Fe_DE_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Fe' and filepath.split('_')[-3] in ['DE', 'DW']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Fe_DE_list != []:
#                     filepath_dict['Fe_DE'] = Fe_DE_list
#                 Fe_DN_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Fe' and filepath.split('_')[-3] in ['DN', 'DS']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Fe_DN_list != []:
#                     filepath_dict['Fe_DN'] = Fe_DN_list
#             elif lid_code in ['LD2', 'LD3', 'L3A', 'L3B']:
#                 Ry_DE_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Ry' and filepath.split('_')[-3] in ['DE', 'DW']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Ry_DE_list != []:
#                     filepath_dict['Ry_DE'] = Ry_DE_list
#                 Na_DE_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Na' and filepath.split('_')[-3] in ['DE', 'DW']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Na_DE_list != []:
#                     filepath_dict['Na_DE'] = Na_DE_list
#                 Ry_DN_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Ry' and filepath.split('_')[-3] in ['DN', 'DS']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Ry_DN_list != []:
#                     filepath_dict['Ry_DN'] = Ry_DN_list
#                 Na_DN_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Na' and filepath.split('_')[-3] in ['DN', 'DS']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Na_DN_list != []:
#                     filepath_dict['Na_DN'] = Na_DN_list
#         elif data_type == 'F':
#             if lid_code == 'LD1':
#                 Fe_DN_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Fe' and filepath.split('_')[-3] in ['DN', 'DS']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Fe_DN_list != []:
#                     filepath_dict['Fe_DN'] = Fe_DN_list
#                 Fe_DE_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Fe' and filepath.split('_')[-3] in ['DE', 'DW']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Fe_DN_list != []:
#                     filepath_dict['Fe_DE'] = Fe_DE_list
#         elif data_type == 'N':
#             if lid_code == 'LD2':
#                 Na_DV_list = [
#                     filepath for filepath in filepath_list if filepath.split('_')[-4] == 'Na'
#                     and filepath.split('_')[-3] == 'DV' and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Na_DV_list != []:
#                     filepath_dict['Na_DV'] = Na_DV_list
#             elif lid_code in ['LD3', 'L3A', 'L3B']:
#                 Na_DE_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Na' and filepath.split('_')[-3] in ['DE', 'DW']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Na_DE_list != []:
#                     filepath_dict['Na_DE'] = Na_DE_list
#                 Na_DN_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Na' and filepath.split('_')[-3] in ['DN', 'DS']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Na_DN_list != []:
#                     filepath_dict['Na_DN'] = Na_DN_list
#         elif data_type == 'W':
#             if lid_code in ['LD1']:
#                 Ry_DE_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Ry' and filepath.split('_')[-3] in ['DE', 'DW']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Ry_DE_list != []:
#                     filepath_dict['Ry_DE'] = Ry_DE_list
#                 Fe_DE_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Fe' and filepath.split('_')[-3] in ['DE', 'DW']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Fe_DE_list != []:
#                     filepath_dict['Fe_DE'] = Fe_DE_list
#                 Ry_DN_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Ry' and filepath.split('_')[-3] in ['DN', 'DS']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Ry_DN_list != []:
#                     filepath_dict['Ry_DN'] = Ry_DN_list
#                 Fe_DN_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Fe' and filepath.split('_')[-3] in ['DN', 'DS']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Fe_DN_list != []:
#                     filepath_dict['Fe_DN'] = Fe_DN_list
#             elif lid_code in ['LD2', 'LD3', 'L3A', 'L3B']:
#                 Ry_DE_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Ry' and filepath.split('_')[-3] in ['DE', 'DW']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Ry_DE_list != []:
#                     filepath_dict['Ry_DE'] = Ry_DE_list
#                 Na_DE_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Na' and filepath.split('_')[-3] in ['DE', 'DW']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Na_DE_list != []:
#                     filepath_dict['Na_DE'] = Na_DE_list
#                 Ry_DN_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Ry' and filepath.split('_')[-3] in ['DN', 'DS']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Ry_DN_list != []:
#                     filepath_dict['Ry_DN'] = Ry_DN_list
#                 Na_DN_list = [
#                     filepath for filepath in filepath_list
#                     if filepath.split('_')[-4] == 'Na' and filepath.split('_')[-3] in ['DN', 'DS']
#                     and begin_time <= int(filepath.split('_')[-2]) < end_time
#                 ]
#                 if Na_DN_list != []:
#                     filepath_dict['Na_DN'] = Na_DN_list
#     else:
#         print('L0_path ERROR!!!')
#     return filepath_dict


def get_file_list(path):
    file_list = []
    for home, dirs, files in os.walk(path):
        for file in files:
            file_list.append(os.path.join(home, file))
    return file_list


def check_filename(filename):
    fileinfo = filename.split("_")


if __name__ == "__main__":
    L2_filepath = r"D:\1.Work\1.Data\新型地基\数据\实测数据\20230920\2023年09月20日数据分享\L2\Density\Atmosphere\AL_LD2_D_20230829140006_L2.dat"
    L0_path = r"D:\1.Work\1.Data\新型地基\数据\实测数据\20230920\2023年09月20日数据分享\L0"
